Skip to contentMethod: toSesameValue(Assertion, Value)
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.sesame;
16:
17: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
18: import cz.cvut.kbss.ontodriver.model.Assertion;
19: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
20: import org.openrdf.model.Value;
21: import org.openrdf.model.ValueFactory;
22:
23: class SesameValueConverter {
24:
25: private ValueFactory vf;
26: private String language;
27:
28: SesameValueConverter(ValueFactory vf, String language) {
29: this.vf = vf;
30: this.language = language;
31: }
32:
33: Value toSesameValue(Assertion assertion, cz.cvut.kbss.ontodriver.model.Value<?> val)
34: throws SesameDriverException {
35:• switch (assertion.getType()) {
36: case DATA_PROPERTY:
37: return SesameUtils.createDataPropertyLiteral(val.getValue(), language, vf);
38: case CLASS:
39: case OBJECT_PROPERTY:
40: return getValueAsSesameUri(val);
41: case ANNOTATION_PROPERTY: // Intentional fall-through
42: case PROPERTY:
43: return resolvePropertyValue(val);
44: default:
45: // Failsafe
46: throw new IllegalArgumentException("Unsupported assertion type " + assertion.getType());
47: }
48: }
49:
50: private org.openrdf.model.URI getValueAsSesameUri(cz.cvut.kbss.ontodriver.model.Value<?> val)
51: throws SesameDriverException {
52: try {
53: return vf.createURI(val.getValue().toString());
54: } catch (IllegalArgumentException e) {
55: throw new SesameDriverException(e);
56: }
57: }
58:
59: private org.openrdf.model.Value resolvePropertyValue(cz.cvut.kbss.ontodriver.model.Value<?> val) {
60: if (SesameUtils.isResourceIdentifier(val.getValue())) {
61: return vf.createURI(val.getValue().toString());
62: } else {
63: return SesameUtils.createDataPropertyLiteral(val.getValue(), language, vf);
64: }
65: }
66: }